import pickle

class Student:
    def __init__(self, cla, no, name, score):
        self.cla = cla
        self.no = no
        self.name = name
        self.score = score


class frame:
    # 读取文件内容并存入列表，用序列化读取文件中的Student对象
    def __init__(self):
        self.stu_list = []
        with open("studentinfo.txt", "rb") as file1:
            while True:
                try:
                    stu_obj = pickle.load(file1)
                    self.stu_list.append(stu_obj)
                except EOFError:
                    break

    def displayallinfo(self):
        print("班级".ljust(10), "学号".ljust(10), "姓名".ljust(10), "python_score")
        for obj in self.stu_list:
            print(obj.cla.ljust(10), obj.no.ljust(10), obj.name.ljust(10), obj.score)

    def add(self):
        stu_info = input("请按格式输入班级，学号，姓名，python分数(物联1902 743 王五 92)").split()
        new_student = Student(stu_info[0], stu_info[1], stu_info[2], int(stu_info[3]))
        # 更新列表
        self.stu_list.append(new_student)
        with open("studentinfo.txt", "ab") as file1:
            pickle.dump(new_student, file1)
            print("添加成功")

    # 按名字查找
    def search(self, name):
        # 记录索引值
        i = 0
        for key in self.stu_list:
            if key.name == name:
                print("查询成功:\n", key.cla.ljust(10), key.no.ljust(10), key.name.ljust(10), key.score)
                return i
            else:
                i += 1
        print("不存在该名字的学生")
        return -1

    # 按名字删除
    def delstu(self, name):
        index = self.search(name)
        if index == -1:
            print("无法删除，原因如上")
            return
        else:
            self.stu_list.pop(index)
            print("已删除")
        # 以可写的方式重置文件内容
        with open("studentinfo.txt", "wb") as file1:
            # 把列表中的对象写到文件中去
            for key in self.stu_list:
                pickle.dump(key, file1)

    # 以名字为索引进行修改
    def update(self, name):
        index = self.search(name)
        if index == -1:
            return
        else:
            # 要更新的学生信息
            update_stu = input("请按格式输入新的学生信息(物联1902 743 王五 92)").split()
            # 创建学生对象
            new_stu = Student(update_stu[0], update_stu[1], update_stu[2], str(update_stu[3]))
            with open("studentinfo.txt", "wb") as file1:
                self.stu_list[index] = new_stu
                for key in self.stu_list:
                    pickle.dump(key, file1)
            print("修改成功!")


if __name__ == '__main__':
    MAIN = frame()
    while True:
        print("*" * 10, "学生成绩管理系统", "*" * 10)
        print("1.增加学生信息")
        print("2.查询学生信息")
        print("3.修改学生信息")
        print("4.删除学生信息")
        print("5.显示所有学生信息")
        print("6.退出系统")
        k = int(input("请输入(1-6)"))
        if k == 1:
            MAIN.add()
        elif k == 2:
            stu_name = input("请输入要查询的学生姓名:")
            MAIN.search(stu_name)
        elif k == 3:
            # 先前的名字
            proname = input("请输入要修改的学生名")
            MAIN.update(proname)
            pass
        elif k == 4:
            del_stu = input("请输入要删除的学生姓名")
            MAIN.delstu(del_stu)
        elif k == 5:
            MAIN.displayallinfo()
        elif k == 6:
            exit(0)
        else:
            print("输入有误请重新输入")

